package jm.demo.bean;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;

import jm.demo.auth.domain.User;

import org.apache.commons.beanutils.BeanUtils;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class MapToBeanDemo {
	
	
	public static void main(String[] args) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		// Should not work
		map.put("name2", "JIMING2");
		map.put("userDetail.university.CounTry", "Kerea");

		// Should work
		map.put("name", "JIMING");
		map.put("userDetail.hometown", "SHAN'XI");
		map.put("userDetail.university.name", "BIT");
		map.put("birthday", new java.sql.Date(347155200*1000));
		
		ObjectMapper mapper = new ObjectMapper();
		User user = User.getUserOne();
		
		try {
			System.out.println(mapper.writeValueAsString(user));
			BeanUtils.populate(user, map);
			System.out.println(mapper.writeValueAsString(user));
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
}
